package com.augurit.wf.common.dao;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.augurit.common.base.dao.BaseDao;
import com.augurit.wf.client.agency.web.form.WfAgencyForm;
import com.augurit.wf.common.constant.WfConstant;
import com.augurit.wf.common.entity.Jbpm4Task;

@Repository
public class Jbpm4TaskDao extends BaseDao<Jbpm4Task, Long> {
 
	public List<Jbpm4Task> getByAssigneeAndProcessInstanceId(String assignee, String processInstanceId){
		String hql = "select task from Jbpm4Task task, Jbpm4Execution execution where task.assignee = ? and task.procinst = execution.dbid and execution.id=?";
		return this.find(hql, assignee, processInstanceId);
	}

	public void clearAgent(Jbpm4Task task){
		if(task != null){
			task.setAgent(null);
			task.setAgentName(null);
			task.setAgentStartDate(null);
			task.setAgentEndDate(null);
			this.save(task);
		}
	}
	
	public void clearAgent(Long taskId){
		if(taskId != null){
			Jbpm4Task task = this.get(taskId);
			this.clearAgent(task);
		}
	}
	
	public void setAgent(Jbpm4Task task, WfAgencyForm form){
		if(task != null && form != null){
			task.setAgent(form.getAgent());
			task.setAgentName(form.getAgentName());
			task.setAgentStartDate(form.getStartDate());
			task.setAgentEndDate(form.getEndDate());
			this.save(task);
		}
	}

	/**
	 * 根据流程实例ID获取对应的Jbpm4Task
	 */
	public List<Jbpm4Task> getTasksByProcInstId(String procInstId) {
		if (procInstId != null && procInstId.trim().length() > 0) {
			String hql = "select task from Jbpm4Task task, Jbpm4HistProcinst hisProcInst"
					+ " where task.procinst = hisProcInst.procInstDbId"
					+ " and hisProcInst.procInstId=?";
			return this.find(hql, procInstId);
		}
		return null;
	}
	
	/**
	 * 根据流程实例ID获取对应的Jbpm4Task
	 */
	public List<Jbpm4Task> getRecentTasksByProcInstDbid(Long procInstDbid) {
		if (procInstDbid != null) {
			String hql = "from Jbpm4Task ps where ps.create between ? and ? and ps.procinst=?";
			
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.SECOND, -10);
			Date startRange = cal.getTime();
			cal.add(Calendar.SECOND, +20);
			Date endRange = cal.getTime();
			
			return this.find(hql, startRange, endRange, procInstDbid);
		}
		return null;
	}

	/**
	 * 修改Jbpm4Task状态
	 */
	public void updateTaskState(List<Jbpm4Task> list, String state){
		if(list != null && list.size() > 0 && state != null && state.trim().length() > 0){
			for(Jbpm4Task entity : list){
				entity.setState(state);
				this.save(entity);
			}
		}
	}
	
	/**
	 * 根据活动实例ID获取活动实例任务列表
	 * @param actInstDbid
	 * @return
	 */
	public List<Jbpm4Task> getTasksByActInstDbid(Long actInstDbid) {
		if (actInstDbid != null) {
			String hql = "select task from Jbpm4Task task, Jbpm4HistActInst hisActInst"
					+ " where (task.taskInstDbid = hisActInst.hisTaskDbid or task.supertask = hisActInst.hisTaskDbid)"
					+ " and hisActInst.dbid = ?";
			return this.find(hql, actInstDbid);
		}
		return null;
	}

	/**
	 * 根据executionId获取多工作项的superTaskId
	 * @param executionId
	 * @return
	 */
	public String getSuperTaskIdByExecutionId(String executionId){
		if(executionId != null && executionId.trim().length() > 0){
			String hql = "select ps.taskInstDbid from Jbpm4Task ps where ps.taskType=? and ps.supertask is null and ps.executionId=?";
			Long taskInstDbid = this.findUnique(hql, WfConstant.TASK_TYPE_MULTITASK, executionId);
			if(taskInstDbid != null)
				return taskInstDbid.toString();
		}
		return null;
	}
}